#include<stdio.h>
#include<errno.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<stdlib.h>

//char* newenv="myVAL=77777777";

char *const addenv[]={
	(char* const)"MYVAL=123456789",
	NULL
};

int main()
{
	printf("我的程序要运行了！\n");
	pid_t id=fork();
	if(id == 0)
	{
		printf("I am child,My Pid Is: %d\n",getpid());
		sleep(1);


		char *const argv[]={
			(char* const)"other",
			(char* const)"-a",
			(char* const)"-b",
			(char* const)"-c",
			(char* const)"-d",
			NULL
		};

		for(int i=0;addenv[i];i++)
		{
			putenv(addenv[i]);//给当前进程增加新的环境变量，不覆盖原始的（不改变原始的环境变量）
		}
		extern char **environ;
		execvpe("./other",argv,environ);//使用全新的环境变量列表，

		//putenv(newenv);

		//execvp("./other",argv);
		//char *const argv[]={
		//(char* const)"ls",
		//(char* const)"-a",
		//(char* const)"-l",
		//NULL
		//};
		//execv("/usr/bin/ls",argv);

		//execlp("ls","ls","-ln","-a",NULL);
		//child
		//execl("/usr/bin/ls","/usr/bin/ls","-ln","-a",NULL);
	    //execl("./other","other",NULL);
		//execl("/usr/bin/python3","python","other.py",NULL);
		//execl("/usr/bin/bash","bash","other.sh",NULL);
		exit(1);
	}

	//parent
	waitpid(-1,NULL,0);
	printf("我的程序运行完毕了！\n");
	return 0;



	//printf("我的程序要运行了!\n");
	//只要exec*系列函数返回，就代表发生错误，无需判断
	//int ret=execl("/usr/bin/a","a","-l","-a",NULL);	
	//execl("/usr/bin/top","top",NULL);
	//printf("我的程序运行完毕了,%d!\n",ret);
	//printf("我的程序要运行了!\n");
	//execl("/usr/bin/ls","ls","-l","-a",NULL);	
	//execl("/usr/bin/top","top",NULL);
	//printf("我的程序运行完毕了!\n");
	return 0;
}
